package com.studynotes.dev.tech.exception;

import cn.hutool.core.text.CharSequenceUtil;
import com.studynotes.dev.tech.common.enums.ErrorCodeEnum;
import lombok.Getter;

/**
 * @author Liu XiaoJun
 * @description: 全局运行时一场
 * @date 2025-11-07 15:17:27
 */
@Getter
public class GlobalException extends RuntimeException {

    private static final long serialVersionUID = -9048304393647572713L;

    /**
     * 错误码
     */
    private transient ErrorCodeEnum errorCode;

    public GlobalException() {
        super();
    }

    public GlobalException(ErrorCodeEnum errorCode) {
        super(errorCode.getMsg());
        this.errorCode = errorCode;
    }

    public GlobalException(String msg) {
        super(msg);
    }

    public GlobalException(String msg, Throwable e) {
        super(msg, e);
    }

    public GlobalException(Throwable e) {
        super(e);
    }

    public GlobalException(ErrorCodeEnum errorCode, String msg) {
        super(msg);
        this.errorCode = errorCode;
    }

    public static GlobalException exception(ErrorCodeEnum errorCode) {
        return new GlobalException(errorCode);
    }

    public static GlobalException exception(ErrorCodeEnum errorCode, String msg) {
        return new GlobalException(errorCode, msg);
    }

    public static GlobalException exception(ErrorCodeEnum errorCode, String template, Object... args) {
        String msg = CharSequenceUtil.format(template, args);
        return new GlobalException(errorCode, msg);
    }

}
